package demo1;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.locks.ReentrantLock;

public class Main0 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter=new Counter();
        ReentrantLock lock=new ReentrantLock();
        Callable<Integer> callable=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                try {
                    lock.lock();
                    for (int i = 0; i <1_0000 ; i++) {
                        counter.sum+=i;
                    }
                    return null;
                } finally {
                    lock.unlock();
                }
            }
        };
        FutureTask futureTask=new FutureTask(callable);
        Thread t1=new Thread(futureTask);
        Thread t2=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lock();
                    for (int i = 0; i <1_0000 ; i++) {
                        counter.sum+=i;
                    }
                } finally {
                    lock.unlock();
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter.sum);

    }
}
class Counter{
    int sum=0;
}
